<template>
  <!-- 实现页面的基本布局 -->
  <el-card class="tree-card">
    <!-- 用了一个行列布局 -->
    <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
    <!-- 下面就是树形控件内容了 -->
    <el-tree :data="departs" :props="defaultProps">
      <tree-tools
        slot-scope="{ data }"
        :tree-node="data"
        @addDepts="addDepts"
        @delDepts="getDepartments"
        @editDepts="editDepts"
      />
    </el-tree>
    <AddDept ref="add-dept" :tree-node="node" @addDepts="getDepartments" />
  </el-card>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept' // 引入新增部门组件
export default {
  name: 'Department',
  components: { treeTools, AddDept },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      departs: [],
      company: { name: '', manager: '' },
      node: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
    },
    // 添加
    addDepts(node) {
      this.node = node
      this.$refs['add-dept'].showDialog = true
    },
    // 修改
    editDepts(node) {
      this.node = node
      this.$refs['add-dept'].showDialog = true
      this.$refs['add-dept'].getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
